extends Skill
class_name BaseAttack


@export var lunge_distance: float = 10.0 # 冲击距离
@export var lunge_duration: float = 0.1 # 前进时间
@export var return_duration: float = 0.05 # 返回时间

func _init() -> void:
	skill_name = "base_attack"
	skill_range = 20
	cooldown = 1
	
func perform_skill_effect() -> void:
	if not caster or not target:
		push_warning("技能缺少caster或target")
		return
	
	# 获取动画目标节点
	var sprite = _get_attacker_sprite()
	if not sprite:
		push_error("找不到caster的Sprite2D节点")
		return
	
	# 记录原始位置
	var original_pos = sprite.position
	var attack_dir = (target.global_position - caster.global_position).normalized()
	
	# 创建并执行动画
	var tween = caster.create_tween().set_parallel(false)
	tween.set_trans(Tween.TRANS_QUAD)
	
	# 冲击阶段
	tween.tween_property(sprite, "position",
		original_pos + attack_dir * lunge_distance, lunge_duration)
	
	# 伤害触发时刻
	tween.tween_callback(func():
		caster.deal_damage(self)
	).set_delay(lunge_duration * 0.8) # 在冲击阶段80%时触发伤害
	
	# 返回阶段
	tween.tween_property(sprite, "position",
		original_pos, return_duration)

func _get_attacker_sprite() -> Sprite2D:
	# 通过类型和节点名称查找Sprite节点
	var sprite = caster.get_node("Sprite2D") as Sprite2D
	if not sprite:
		for child in caster.get_children():
			if child is Sprite2D:
				return child
	return sprite
